# -*- coding: utf-8 -*-
"""
TG卡片组件 - TgCard
卡片组件，由基础组件组合而成的复杂组件
"""
from PySide6.QtWidgets import QWidget
from ...style_manager import StyleManager
from ..inputs import TgLabel
from ..containers import TgFrame, TgLayout


class TgCard(QWidget):
    """TG卡片组件 - 组合基础组件创建的复杂组件"""
    
    def __init__(self, title="", content="", parent=None):
        super().__init__(parent)
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        self._component_parts = {}
        
        # 设置组件特定属性
        self.title_text = title
        self.content_text = content
        
        # 初始化组件
        self.initialize()
    
    @property
    def component_name(self):
        """获取组件名称"""
        return self._component_name
    
    @property
    def is_initialized(self):
        """检查组件是否已初始化"""
        return self._is_initialized
    
    @property
    def style_applied(self):
        """检查样式是否已应用"""
        return self._style_applied
    
    def setup_ui(self):
        """初始化UI"""
        layout = TgLayout(margins='medium', spacing='small')
        
        # 创建卡片框架
        card_frame = TgFrame('panel')
        card_layout = TgLayout(margins='medium', spacing='small')
        
        # 添加标题
        if self.title_text:
            title_label = TgLabel(self.title_text, 'title', 'primary')
            card_layout.addWidget(title_label)
            self.add_component_part('title', title_label)
        
        # 添加内容
        if self.content_text:
            content_label = TgLabel(self.content_text, 'description')
            card_layout.addWidget(content_label)
            self.add_component_part('content', content_label)
        
        card_frame.setLayout(card_layout)
        layout.addWidget(card_frame)
        self.setLayout(layout)
        
        self.add_component_part('frame', card_frame)
    
    def apply_style(self):
        """应用样式"""
        # 设置卡片样式
        card_frame = self.get_component_part('frame')
        if card_frame:
            card_frame.setStyleSheet(f"""
                QFrame {{
                    border: 1px solid {StyleManager.COLORS['border']};
                    border-radius: {StyleManager.BORDER_RADIUS['large']};
                    background-color: {StyleManager.COLORS['background']};
                    padding: {StyleManager.SPACING['lg']};
                }}
                QFrame:hover {{
                    border: 2px solid {StyleManager.COLORS['primary']};
                    background-color: {StyleManager.COLORS['background_light']};
                }}
            """)
        self._style_applied = True
    
    def initialize(self):
        """初始化组件"""
        if not self._is_initialized:
            self.setup_ui()
            self.apply_style()
            self._is_initialized = True
            self._style_applied = True
    
    def add_component_part(self, name, component):
        """添加组件部分"""
        self._component_parts[name] = component
    
    def get_component_part(self, name):
        """获取组件部分"""
        return self._component_parts.get(name)
    
    def get_component_parts(self):
        """获取所有组件部分"""
        return self._component_parts.copy() 